package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mapper.GoodsMapper;
import com.example.model.Goods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private GoodsMapper goodsMapper;
    /**
     * 获取所有的商品信息
     * @return
     */
    @GetMapping
    public List<Goods> list(){
        List<Goods> goods = goodsMapper.selectList(null);
        return goods;
    }

    /**
     * 分页查询商品信息
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public Page<Goods> page(@PathVariable("currentPage") Integer pageNum,
                            @PathVariable("pageSize") Integer pageSize){
        //构建一个分页参数
        Page<Goods> page = new Page<>(pageNum, pageSize);
        Page<Goods> goodsPage = goodsMapper.selectPage(page, null);
        return goodsPage;
    }


    /**
     * 实现添加商品
     * 接收json对象
     * @RequestBody: 将前端传递的json对象转为java对象
     */
    @PostMapping
    public String add(@RequestBody Goods goods){
       goodsMapper.insert(goods);
       return "success";
    }
    /**
     * 根据商品的编号更新商品信息
     */
    @PutMapping
    public String update(@RequestBody Goods goods){
        goodsMapper.updateById(goods);
        return "success";
    }
    /**
     * 根据商品的编号删除对应的商品信息
     */
    @DeleteMapping("/{id}")
    public String delete(@PathVariable("id") Integer id){
        goodsMapper.deleteById(id);
        return "success";
    }

}
